package cn.tristenstudy.blog.controller.admin;

import cn.tristenstudy.blog.exception.MyNotFoundException;
import cn.tristenstudy.blog.pojo.Tag;
import cn.tristenstudy.blog.service.TagService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 分类后台管理
 *
 * @author xichen1.li
 * @date 2020-12-28 11:31
 */
@Controller
@RequestMapping("/admin/tag")
public class TagManagerController {

    @Autowired
    private TagService tagService;

    /**
     * 分页查询
     *
     * @param current 分页条件
     * @param model   model对象
     * @return 页面
     */
    @GetMapping("/{current}/list")
    public String list(@PathVariable Long current, Model model) {
        if (current <= 0) {
            throw new MyNotFoundException("无法找到页面！");
        }
        Page<Tag> result = tagService.listTag(current);
        model.addAttribute("page", result);
        model.addAttribute("first", result.hasPrevious());
        model.addAttribute("last", result.hasNext());
        return "admin/tags";
    }

    /**
     * 跳转至新增页面
     */
    @GetMapping("/input")
    public String input(Model model) {
        model.addAttribute("tag", new Tag());
        return "admin/tags-input";
    }

    /**
     * 跳转至修改页面
     */
    @GetMapping("/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        Tag tag = tagService.getTag(id);
        model.addAttribute("tag", tag);
        return "admin/tags-input";
    }

    /**
     * 新增标签
     *
     * @param tag        标签
     * @param attributes attributes
     */
    @PostMapping("/add")
    public String add(Tag tag, RedirectAttributes attributes) {
        boolean exist = tagService.isNotExist(tag);
        if (!exist) {
            attributes.addFlashAttribute("msg", "该标签名存在！");
            return "redirect:/admin/tag/input";
        }
        Boolean temp = tagService.saveTag(tag);
        if (temp) {
            attributes.addFlashAttribute("msg", "新增成功！");
        } else {
            attributes.addFlashAttribute("msg", "新增失败！");
        }
        return "redirect:/admin/tag/1/list";
    }

    /**
     * 修改标签
     *
     * @param tag        标签
     * @param attributes attributes
     */
    @PostMapping("/edit")
    public String edit(Tag tag, RedirectAttributes attributes) {
        boolean exist = tagService.isNotExist(tag);
        if (!exist) {
            attributes.addFlashAttribute("msg", "该标签已存在！");
            return "redirect:/admin/tag/" + tag.getId() + "/input";
        }
        Boolean temp = tagService.updateTag(tag);
        if (temp) {
            attributes.addFlashAttribute("msg", "更新成功！");
        } else {
            attributes.addFlashAttribute("msg", "更新失败！");
        }
        return "redirect:/admin/tag/1/list";
    }

    /**
     * 删除指定标签
     *
     * @param id         id
     * @param attributes attributes
     */
    @GetMapping("/{id}/{current}/delete")
    public String delete(@PathVariable Long id, @PathVariable int current, RedirectAttributes attributes) {
        Boolean result = tagService.deleteTag(id);
        if (result) {
            attributes.addFlashAttribute("msg", "删除成功！");
            return "redirect:/admin/tag/" + current + "/list";
        } else {
            attributes.addFlashAttribute("msg", "删除失败！");
            return "redirect:/admin/tag/" + current + "/list";
        }

    }

}
